from dataclasses import dataclass

DEFAULT_CODE = 999
DEFAULT_MSG = 'unknown error'


TEST_CODE = 100


class Dicter(object):
    def __iter__(self):
        for key in self.__dict__:
            value = getattr(self, key)
            if value is not None:
                if isinstance(value, Dicter):
                    yield key, dict(value)
                else:
                    yield key, value


class MyError(Exception, Dicter):
    def __init__(self, code, msg, detail=None):
        self.code = code
        self.msg = msg
        self.detail = detail


class SimpleError(Exception, Dicter):
    def __init__(self, msg):
        self.msg = msg


@dataclass
class BaseError:
    def get_code(self) -> int:
        return self.code

    def get_msg(self) -> str:
        return self.msg


class DefaultError(BaseError):
    code: int = DEFAULT_CODE
    msg: str = DEFAULT_MSG
